import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:syn_4s/core/extension/build_context_extension.dart';
import 'package:syn_4s/core/extension/int_extension.dart';
import 'package:syn_4s/core/model/un_safe_act_model.dart';
import 'package:syn_4s/core/services/config.dart';
import 'package:syn_4s/ui/shared/app_theme.dart';

import '../../core/utils/date_format_utils.dart';
import '../../core/viewmodel/main_view_model.dart';
import 'custom_ui.dart';
import 'main_fold.dart';

class MainUnsafeAct extends StatefulWidget {
  final Key? scrollKey;
  final MainViewModel viewModel;
  final String? title;

  const MainUnsafeAct({super.key, required this.viewModel, this.scrollKey,this.title});

  @override
  State<MainUnsafeAct> createState() => _MainUnsafeActState();
}

class _MainUnsafeActState extends State<MainUnsafeAct> {
  final ScrollController _controller = ScrollController();

  @override
  void initState() {
    super.initState();
    addScrollListener(_controller, widget.viewModel.isUnsafeScrollable);
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MainFold(
      scrollKey: widget.scrollKey,
      title: widget.title??context.l10n.unsafe_act,
      child: Container(
        margin: EdgeInsets.only(left: 8.px, right: 8.px, bottom: 6.px),
        height: 140.px,
        child: Obx(() => ListView.builder(
              controller: _controller,
              physics: widget.viewModel.isUnsafeScrollable.value
                  ? const AlwaysScrollableScrollPhysics()
                  : const NeverScrollableScrollPhysics(),
              itemBuilder: (ctx, index) {
                return buildItem(context, widget.viewModel.unSafeActs[index]);
              },
              itemCount: widget.viewModel.unSafeActs.length,
            )),
      ),
    );
  }

  Widget buildItem(BuildContext context, UnSafeActModel model) {
    return Container(
      height: 130.px,
      width: double.infinity,
      color: AppTheme.unsafeActColor,
      margin: EdgeInsets.only(bottom: 8.px),
      padding: EdgeInsets.all(8.px),
      child: Row(
        children: [
          Image.network(
            "${HttpConfig.imageURL}${model.photo ?? ""}",
            width: 160.px,
              errorBuilder: (ctx, e, stackTrace) {
                return Container(width: 160.px);
              }
          ),
          // Container(
          //   width: 160.px,
          //   color: Colors.orange,
          // ),
          SizedBox(
            width: 12.px,
          ),
          buildInfo(context, model.name, model.portalAt),
        ],
      ),
    );
  }

  Widget buildInfo(BuildContext context, String? name, DateTime? time) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        buildText(
          name ?? "",
        ),
        SizedBox(
          height: 28.px,
        ),
        buildText(
          DateFormatUtils.dateTimeToStr(time, format: "dd-MM-yyyy HH:mm:ss"),
        )
      ],
    );
  }

  Widget buildText(String data) {
    return Text(
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
      data,
      style: const TextStyle(
        color: Colors.white,
      ),
    );
  }
}
